package com.jadeen.controller.clientController;

import cn.dev33.satoken.stp.StpUtil;
import com.jadeen.common.controller.BaseController;
import com.jadeen.common.domain.AjaxResult;
import com.jadeen.entity.domain.User;
import com.jadeen.entity.dto.PasswordDto;
import com.jadeen.entity.dto.SignUpDto;
import com.jadeen.service.IUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 用户信息Controller
 * 
 * @author jadeen
 * @date 2025-08-27
 */
@RestController
@RequestMapping("/client/share/user")
public class ClientUserController extends BaseController
{
    @Resource(name = "userServiceClient")
    private IUserService userService;
    /**
     * 获取用户信息详细信息
     */
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(userService.selectUserByUserId(userId));
    }


    /**
     * 获取用户信息
     */
    @GetMapping(value = "/getUserProfile")
    public AjaxResult getUserProfile()
    {
        return success(userService.getUserProfile());
    }

    /**
     * 修改用户信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }
    /**
     * 修改用户信息
     */
    @PutMapping("/profile")
    public AjaxResult editProfile(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }
    /**
     * 修改用户密码
     */
    @PutMapping("/profile/updatePwd")
    public AjaxResult editProfileUpdatePwd( PasswordDto password)
    {
        return userService.editProfileUpdatePwd(password);
    }

    /**
     * 登录
     */
    @PostMapping("/doLogin")
    public AjaxResult login(@RequestBody User user){
        return userService.login(user);
    }
    /**
     * 注册
     */
    @PostMapping("/doSignUp")
    public AjaxResult signUp(@RequestBody SignUpDto signUpDto){
        return userService.signUp(signUpDto);
    }
    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    @PostMapping("/profile/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        return  userService.avatar(file);

    }
    @GetMapping("/logout")
    public void logout()
    {
        System.out.println(StpUtil.isLogin());
        User user = new User();
        user.setUserId(StpUtil.getLoginIdAsLong());
        user.setLastLoginTime(new Date());
        userService.updateUser(user);
        StpUtil.logout();
    }

}
